﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML dir=ltr XMLNS:MSHelp = "http://msdn.microsoft.com/mshelp" xmlns:ddue = 
"http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink = 
"http://www.w3.org/1999/xlink" xmlns:tool = "http://www.microsoft.com/tooltip" 
XMLNS:[default] http://ddue.schemas.microsoft.com/authoring/2003/5 = 
"http://ddue.schemas.microsoft.com/authoring/2003/5"><HEAD><TITLE>Message Buffer Client Sample</TITLE>
<META content="text/html; CHARSET=utf-8" http-equiv=Content-Type></META>
<META name=save content=history></META>
<LINK rel=stylesheet type=text/css 
href="../../../../CommonFiles/Classic.css"></LINK>

<META name=GENERATOR content="MSHTML 8.00.6001.18783">
<style type="text/css">
.style2 {
				font-family: monospace;
				font-size: small;
				color: #000000;
}
.style3 {
				font-weight: 600;
				font-size: 125%;
				color: #003399;
}
</style>
</HEAD>
<BODY><INPUT id=userDataCache class=userDataStyle type=hidden></INPUT><INPUT 
id=hiddenScrollOffset type=hidden></INPUT><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=dropDownImage 
src="../../../../../Common/Html/drpdown.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=dropDownHoverImage 
src="../../../../../Common/Html/drpdown_orange.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=copyImage 
src="../../../../../Common/Html/copycode.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=copyHoverImage 
src="../../../../../Common/Html/copycodeHighlight.gif"></IMG>
<DIV id=header>
<TABLE id=topTable width="100%">
  <TBODY>
  <TR id=headerTableRow1>
    <TD align=left><SPAN id=runningHeaderText></SPAN></TD></TR>
  <TR id=headerTableRow2>
    <TD align=left><span class="style3">Message Buffer Client</span><SPAN id=nsrTitle> Sample</SPAN></TD></TR>
  <TR id=headerTableRow3>
    <TD></TD></TR></TBODY></TABLE>
</DIV>
<DIV id=mainSection>
<DIV id=mainBody>
<DIV id=allHistory class=saveHistory onload="loadAll()" 
onsave="saveAll()"></DIV>
<P>This sample demonstrates how to use the Windows Azure Service Bus and its message buffer feature. </P>
<P>The sample shows two console applications: the first is a producer of messages (Producer), the second is a consumer of messages (Consumer). 
The Consumer application creates the message buffer and then 
waits to read messages from the message buffer. The Producer application retrieves the 
message buffer, then sends messages to it. </P>
<H2 class=heading>Prerequisites</H2>
<DIV id=sectionSection0 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">If you haven&#39;t already done so, please read the release notes 
document that explains how to sign up for a Windows Azure account and 
how to configure your environment.</P>
				</content>
</DIV>
<H2 class=heading>The Consumer</H2>
<DIV id=sectionSection1 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">The Consumer application creates the message buffer and then retrieves messages from it that are sent by the Producer.
Creating a message buffer requires the credentials for the endpoint, the message buffer URI, and the message buffer policy. The Consumer creates these
prerequisite objects.
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>Console.Write("Your Service Namespace: ");
string serviceNamespace = Console.ReadLine();
Console.Write("Your Issuer Name: ");
string issuerName = Console.ReadLine();
Console.Write("Your Issuer Secret: ");
string issuerSecret = Console.ReadLine();

// create the policy for the message buffer
this.policy = new MessageBufferPolicy();
this.policy.Authorization = AuthorizationPolicy.Required;
this.policy.MaxMessageCount = 10;
this.policy.ExpiresAfter = TimeSpan.FromMinutes(5); // messages in the message buffer expire after 5 mins
this.policy.TransportProtection = TransportProtectionPolicy.AllPaths;

// create the credentials object for the endpoint
this.credential = new TransportClientEndpointBehavior();
this.credential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);

// create the URI for the message buffer
this.uri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "MessageBuffer");           
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>

<P xmlns="">After creating the credentials for the endpoint, the message buffer URI, and the message buffer policy the 
Consumer creates the message buffer.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>this.client = MessageBufferClient.CreateMessageBuffer(this.credential, this.uri, this.policy);
Console.WriteLine("Message buffer created at '{0}'.", this.client.MessageBufferUri);
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">After the message buffer is created and the Producer has sent messages to this message buffer, the Consumer
retrieves the first two messages.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>Message retrievedMessage;

// retrieve the first two messages from the message buffer
Console.Write("Press [Enter] to retrieve the first message in the message buffer: ");
Console.ReadLine();
retrievedMessage = this.client.Retrieve();
Console.WriteLine("Retrieved message with body '{0}'.", retrievedMessage.GetBody&lt;string&gt;());
retrievedMessage.Close();

Console.Write("Press [Enter] to retrieve the next message from the message buffer: ");
Console.ReadLine();
retrievedMessage = this.client.Retrieve();
Console.WriteLine("Retrieved message with body '{0}'.", retrievedMessage.GetBody&lt;string&gt;());
retrievedMessage.Close();
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">Next, the Consumer peek/locks the last message and deletes this locked message.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>// lock and peek at the next message; then delete it
Console.Write("Press [Enter] to lock, peek, and delete the next message from the message buffer: ");
Console.ReadLine();
Message lockedMessage = this.client.PeekLock();
this.client.DeleteLockedMessage(lockedMessage);
Console.WriteLine("Message with body '{0}' locked, peeked, and then deleted.", lockedMessage.GetBody&lt;string&gt;());
lockedMessage.Close();
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">After retrieving all messages, the Consumer deletes the message buffer.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>// delete the message buffer
this.client.DeleteMessageBuffer();
Console.WriteLine("Message buffer at {0} was deleted.", this.client.MessageBufferUri);
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
</content></DIV>
<H2 class=heading>The Producer</H2>
<DIV id=DIV1 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">The Producer application accesses the message buffer that the Consumer created and sends three messages to it. 
Connecting to a message buffer requires the credentials for the endpoint and the message buffer URI. The Producer creates these prerequisite 
objects first.
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>Console.Write("Your Service Namespace: ");
string serviceNamespace = Console.ReadLine();
Console.Write("Your Issuer Name: ");
string issuerName = Console.ReadLine();
Console.Write("Your Issuer Secret: ");
string issuerSecret = Console.ReadLine();

// create the credentials object for the endpoint
this.credential = new TransportClientEndpointBehavior();
this.credential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);

// create the URI for the message buffer
this.uri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "MessageBuffer");         
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">After creating the endpoint credentials and the message buffer URI, the Producer connects to the message buffer.
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>this.client = MessageBufferClient.GetMessageBuffer(this.credential, this.uri);
Console.WriteLine("Connected to the message buffer at '{0}'.", this.client.MessageBufferUri);       
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">After establishing a connection to the message buffer, the Producer sends three messages to it.
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"></SPAN>// send three messages to the message buffer
for (int i = 1; i <= 3; ++i)
{
    Message message = Message.CreateMessage(MessageVersion.Default, string.Empty, string.Format("&lt;&lt;MESSAGE {0}&gt;&gt;", i));
    this.client.Send(message);
    Console.WriteLine("Sent message with body '&lt;&lt;MESSAGE {0}&gt;&gt;' to the message buffer.", i);
    message.Close();
}              
</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
</content></DIV>
<H2 class=heading>Running the Sample</H2>
<DIV id=sectionSection4 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">To
run the sample, build the solution in Visual Studio or from the command line,
then run the Consumer application first to create the message buffer. The
Producer application accesses this message buffer later on to send messages to it that are
later retrieved by the Consumer.</P>
<P xmlns="">After
starting the Consumer application, it prompts you for the service namespace and
credentials. Enter these values in the Consumer console window. Next, press
ENTER to create the message buffer. This message buffer is used by the Producer
to send messages to.</P>
<P xmlns="">After
the message buffer has been created, run the Producer application. In the
Producer console window, enter the service namespace and credentials. Then
press ENTER to connect to the message buffer previously created by the Consumer.
Next, press ENTER three times to send three messages to the message buffer.</P>
<P xmlns="">After
having sent the messages to the message buffer, switch back to the Consumer
console window and press ENTER to retrieve the first message. The body of the first
message is displayed. Then, press ENTER to retrieve the next message. The body
of the second message is displayed. Next, press ENTER to lock, peek, and delete
the last message. The body of the last message is displayed along with the
operation carried out. Finally, press ENTER to delete the message buffer.</P>
<P xmlns="">Close
both console windows to end the Consumer and Producer applications.</P>
<P xmlns="">The following is the expected output for the Consumer and Producer applications.</P>
<P xmlns=""><B>Expected Output</B><strong> -</strong><B> </B><strong>Consumer</strong></P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TD colSpan=2><PRE class="style2">THE CONSUMER
Your Service Namespace: &lt;service-namespace&gt;
Your Issuer Name: &lt;issuer-name&gt;
Your Issuer Secret: &lt;issuer-secret&gt;
Press [Enter] to create the message buffer:
Message buffer created at &#39;http://&lt;service-namespace&gt;.servicebus.windows.net/MessageBuffer/&#39;.
Press [Enter] to retrieve the first message in the message buffer:
Retrieved message with body &#39;&lt;&lt;MESSAGE 1&gt;&gt;&#39;.
Press [Enter] to retrieve the next message from the message buffer:
Retrieved message with body &#39;&lt;&lt;MESSAGE 2&gt;&gt;&#39;.
Press [Enter] to lock, peek, and delete the next message from the message buffer:
Message with body &#39;&lt;&lt;MESSAGE 3&gt;&gt;&#39; locked, peeked, and then deleted.
Press [Enter] to delete the message buffer:
Message buffer at http://&lt;service-namespace&gt;.servicebus.windows.net/MessageBuffer/ was deleted.
Press any key to continue . . .</PRE>
	</TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns=""><B>Expected Output - Producer</B></P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TD colSpan=2><PRE class="style2">THE PRODUCER
Your Service Namespace: &lt;service-namespace&gt;
Your Issuer Name: &lt;issuer-name&gt;
Your Issuer Secret: &lt;issuer-name&gt;
Press [Enter] to retrieve the message buffer:
Message buffer at &#39;http://&lt;service-namespace&gt;.servicebus.windows.net/MessageBuffer/&#39; retrieved.
Press [Enter] to send messages to the message buffer:
Sent message with body &#39;&lt;&lt;MESSAGE 1&gt;&gt;&#39; to the message buffer.
Sent message with body &#39;&lt;&lt;MESSAGE 2&gt;&gt;&#39; to the message buffer.
Sent message with body &#39;&lt;&lt;MESSAGE 3&gt;&gt;&#39; to the message buffer.
Press any key to continue . . .</PRE></TD></TR></TBODY></TABLE></SPAN></DIV></content></DIV><!--[if gte IE 5]><tool:tip 
avoidmouse="false" element="languageFilterToolTip"></tool:tip><![endif]--></DIV>
<P xmlns="">
    <hr /> 
    Did you find this information useful?
    <a href="http://go.microsoft.com/fwlink/?LinkID=155664">
        Please send your suggestions and comments about the documentation.

    </a></P></DIV></BODY></HTML>
